﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
using System.Reflection;
using UMC.Wiki.Entities;
using UMC.Web;

namespace UMC.Wiki.Activities
{
    class SubjectCommentActivity : WebActivity
    {
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {

            var refer_id = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Refer", g =>
            {
                return new Web.UITextDialog() { Title = "评论的主题" };
            }), 0);

            var user = this.Context.Token.Identity(); 
            if (user.IsAuthenticated == false)
            {
                this.Prompt("不支持匿名评论，请登录", false);
                this.Context.Send("Login", true);

            }

            var Id = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Id", g =>
            {
                return this.DialogValue(UMC.Data.Utility.NewId().ToString());
            }), 0);
            var commant = DataFactory.Instance().Comment(refer_id);

            var ui = this.AsyncDialog("UI", g => this.DialogValue("none"));
            var section = this.AsyncDialog("section", g => this.DialogValue("1"));
            var row = this.AsyncDialog("row", g => this.DialogValue("1"));

            var Content = Web.UIDialog.AsyncDialog(this.Context, "Content", g =>
             {
                 if (request.IsApp == false)
                 {
                     var sn = new WebMeta(request.Arguments);
                     sn.Put("Id", Id).Put("Refer", refer_id);
                     var ms = new WebMeta().Put("submit", new UIClick(request.Model, request.Command, sn) { Text = commant != null ? "写下你的回复..." : "写下你的评论..." });

                     this.Context.Send(commant == null ? "Comment" : "Reply", ms, true);
                 }
                 var from = new Web.UICommentDialog();
                 if (commant != null)
                 {
                     from.Title = "回复评论";
                     from.BottomText = "写下你的回复...";
                 }
                 else
                 {
                     from.IsPicture = true;
                     from.Title = "评论";
                     from.BottomText = "写下你的评论...";
                 }

                 from.SubmitText = commant != null ? "回复" : "评论";
                 return from;
             });
            var image = this.AsyncDialog("Content_Image", "none");
            int imageIndex = 0;
            if (image != "none")
            {
                var webr = UMC.Web.WebResource.Instance();
                var imgs = image.Split(',');
                for (var i = 0; i < imgs.Length; i++)
                {
                    imageIndex |= i + 1;
                    webr.Transfer(new Uri(imgs[i]), Id, i + 1);
                }
            }
            if (commant != null)
            {
                if (commant.Id == Id)
                {
                    return;
                }
                var forId = commant.for_id ?? 0;// Guid.Empty;
                if (forId == 0)
                {
                    forId = commant.Id.Value;
                }

                DataFactory.Instance().Put(new Comment
                {
                    Id = forId,
                    Reply = (commant.Reply ?? 0) + 1
                }, false);
                var sinle = DataFactory.Instance().Subject(commant.ref_id.Value);

                DataFactory.Instance().Put(new Comment
                {
                    Id = Id,
                    CommentDate = DateTime.Now,
                    for_id = forId,
                    ref_id = commant.ref_id,
                    Content = Content,
                    Effective = 0,
                    Farworks = 0,
                    Invalid = 0,
                    project_id = sinle != null ? sinle.project_id : null,
                    Reply = 0,
                    Score = 0,
                    Unhealthy = 0,
                    Visible = 0,
                    ImageIndexs = imageIndex,
                    Poster = user.Alias,
                    user_id = user.Id
                }, true);


                var editer = new Web.UISection.Editer(section, row);
                editer.Put(Utility.Comment(commant, request.Model, request.Url), false);
                editer.SendTo(this.Context, ui, true);

            }
            else
            {
                var sinle = DataFactory.Instance().Subject(refer_id);

                var putCmd = new Comment
                {
                    Id = Id,
                    CommentDate = DateTime.Now,
                    for_id = 0,
                    ref_id = refer_id,
                    project_id = sinle != null ? sinle.project_id : null,
                    Content = Content,
                    Effective = 0,
                    Farworks = 0,
                    Invalid = 0,
                    Reply = 0,
                    Score = 0,
                    Unhealthy = 0,
                    Visible = 0,
                    Poster = user.Alias,
                    user_id = user.Id
                };

                DataFactory.Instance().Put(putCmd, true);
                if (sinle != null)
                {

                    DataFactory.Instance().Put(new Subject
                    {
                        Id = Id,
                        Reply = (sinle.Reply ?? 0) + 1,
                        last_command_id = Id
                    });

                    var ax = DataFactory.Instance().Attention(user.Id.Value) ?? new Attention { Id = user.Id };
                    DataFactory.Instance().Put(new Attention
                    {
                        Id = user.Id,
                        SubjectReplyTotal = (ax.SubjectReplyTotal ?? 0) + 1
                    });

                    var project = DataFactory.Instance().Project(sinle.portfolio_id.Value);

                    if (project != null)
                    {
                        DataFactory.Instance().Put(new Project
                        {
                            Id = project.Id,
                            ReplyTotal = (project.ReplyTotal ?? 0) + 1
                        });
                    }
                    var editer = new Web.UISection.Editer(1, 1);

                    if (sinle.last_command_id.HasValue == false)
                    {
                        editer.Put(Utility.Comment(putCmd, request.Model, request.Url), false);
                    }
                    else
                    {
                        editer.Insert(Utility.Comment(putCmd, request.Model, request.Url));
                    }

                    editer.SendTo(this.Context, ui, true);

                }
            }



        }
    }
}